package zust.se.delicious.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import zust.se.delicious.domain.User;
import zust.se.delicious.service.UserService;
import zust.se.delicious.vo.SessionUserBean;
import zust.se.delicious.vo.UserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SettingAction extends ActionSupport {
	private UserService userService;
	private UserBean user;
	private String oldPassword;
	private String newPassword;
	private String tabType;

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	private String intro;
	private String email;
	private File avatar;
	private String avatarFileName;
	private String avatarContentType;
	private boolean infoSuccessFlag = false;
	private boolean infoEditFlag = false;
	private boolean pwdSuccessFlag = false;

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public boolean isInfoSuccessFlag() {
		return infoSuccessFlag;
	}

	public void setInfoSuccessFlag(boolean infoSuccessFlag) {
		this.infoSuccessFlag = infoSuccessFlag;
	}

	public boolean isInfoEditFlag() {
		return infoEditFlag;
	}

	public void setInfoEditFlag(boolean infoEditFlag) {
		this.infoEditFlag = infoEditFlag;
	}

	public boolean isPwdSuccessFlag() {
		return pwdSuccessFlag;
	}

	public void setPwdSuccessFlag(boolean pwdSuccessFlag) {
		this.pwdSuccessFlag = pwdSuccessFlag;
	}

	public boolean isPwdEditFlag() {
		return pwdEditFlag;
	}

	public void setPwdEditFlag(boolean pwdEditFlag) {
		this.pwdEditFlag = pwdEditFlag;
	}

	private boolean pwdEditFlag = false;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getSavePath() {
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload");
		return savePath;
	}

	public String execute() {
		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionuser = (SessionUserBean) session.get("sessionUser");
		if (sessionuser != null) {
			user = userService.getHomeUser(sessionuser.getId());
			return SUCCESS;
		} else
			return ERROR;
	}

	public String updateInfo() throws IOException {
		tabType = "infoTab";
		Map session = ActionContext.getContext().getSession();
		SessionUserBean user = (SessionUserBean) session.get("sessionUser");
		if (user != null) {
			String saveName = getSaveName(avatarFileName);
			if (saveName != null) {
				File fileToCreate = new File(getSavePath(), saveName);
				FileUtils.copyFile(avatar, fileToCreate);
			}
			User storeUser = new User();
			storeUser.setId(user.getId());
			storeUser.setEmail(email);
			storeUser.setIntroduce(intro);
			storeUser.setAvatar(saveName);
			userService.updateUserInfo(storeUser);
			return SUCCESS;
		} else
			return ERROR;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public File getAvatar() {
		return avatar;
	}

	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}

	public String getAvatarFileName() {
		return avatarFileName;
	}

	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}

	public String getAvatarContentType() {
		return avatarContentType;
	}

	public void setAvatarContentType(String avatarContentType) {
		this.avatarContentType = avatarContentType;
	}

	public String updatePwd() {
		tabType = "pwdTab";
		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionuser = (SessionUserBean) session.get("sessionUser");
		if (sessionuser != null) {
			if (userService.valPassword(sessionuser.getId(), oldPassword, newPassword))
				return SUCCESS;
		}
		return ERROR;
	}

	private String getSaveName(String picName) {
		if (picName == null) {
			return null;
		}
		UUID uuid = UUID.randomUUID();
		String name = uuid.toString() + picName.substring(picName.lastIndexOf('.'), picName.length());

		return name;
	}

}
